<%--
  Created by IntelliJ IDEA.
  User: dzw
  Date: 2017/12/8
  Time: 15:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
  String jsessionid = session.getId();
%>

<div class="panel">
  <div class="panel-heading header">
    <h3 class="panel-title" id="title">物品申请</h3>
  </div>

  <!-- BASIC FORM ELEMENTS -->
  <!--===================================================-->
  <form id="form_goodsApply_input" class="form-horizontal">

    <input type="hidden" name="keyId" id="keyId" value="${goodsApply.id}"/>

    <div class="form-group">
      <label class="col-md-2 control-label">物品用途<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <input type="text" name="purpose" id="purpose" class="form-control" placeholder="请输入物品用途" value="${goodsApply.purpose}">
      </div>
    </div>

    <div class="form-group">
      <div class="col-md-offset-2 col-md-6">
        <table class="table">
          <thead>
          <tr>
            <th>物品名称</th>
            <th>单价（元）</th>
            <th>数量</th>
            <th>小计金额</th>
            <th><btn id="add_line" class="btn btn-primary btn-sm pull-right">增加</btn></th>
          </tr>
          </thead>
          <tbody id="goodsApplyTbody">
          <c:if test="${goodsApply==null || fn:length(goodsApply.goodsApplyDetailList)==0}">
            <tr>
              <td><input type="text" class="form-control" name="name" /></td>
              <td><input onkeyup="calculate(this)" type="number" class="form-control" name="price" /></td>
              <td><input onkeyup="calculate(this)" type="number" class="form-control" name="count" /></td>
              <td><input disabled type="number" class="form-control" name="total" /></td>
              <td><btn onClick="delLine(this)" class="btn btn-danger btn-sm pull-right">删除</btn></td>
            </tr>
          </c:if>
          <c:if test="${goodsApply!=null && fn:length(goodsApply.goodsApplyDetailList)>0}">
            <c:forEach var="detail" items="${goodsApply.goodsApplyDetailList}">
              <tr id="${detail.id}">
                <td><input type="text" class="form-control" name="name" value="${detail.name}" /></td>
                <td><input onkeyup="calculate(this)" type="number" class="form-control" name="price" value="${detail.price}" /></td>
                <td><input onkeyup="calculate(this)" type="number" class="form-control" name="count" value="${detail.count}" /></td>
                <td><input disabled type="number" class="form-control" name="total" value="${detail.total}" /></td>
                <td><btn onClick="delLine(this)" class="btn btn-danger btn-sm pull-right">删除</btn></td>
              </tr>
            </c:forEach>
          </c:if>
          </tbody>
        </table>
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-2 control-label">合计金额<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <input disabled type="number" name="numMoney" id="numMoney" class="form-control" placeholder="自动计算" value="${goodsApply.numMoney}">
      </div>
    </div>

    <div class="form-group useDiv" style="display: none">
      <label class="col-md-2 control-label">领用时间<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <input type="text" name="useDate" id="useDate" class="form-control" placeholder="请选择领用时间" value="<fmt:formatDate pattern="yyyy-MM-dd" value="${goodsApply.useDate}" />" >
      </div>
    </div>

    <div class="form-group useDiv" style="display: none">
      <label class="col-md-2 control-label">领用情况<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <input type="text" name="useCondition" id="useCondition" class="form-control" placeholder="请输入领用情况" value="${goodsApply.useCondition}">
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-2 control-label">
        是否抄送
      </label>
      <div class="col-md-6">
        <div class="radio">
          <input type="radio" id="remindYes" name="remind" value="1" class="magic-radio">
          <label for="remindYes">是</label>
          <input type="radio" id="remindNo" name="remind" value="0" class="magic-radio">
          <label for="remindNo">否</label>
        </div>
      </div>
    </div>

    <div class="form-group" style="display: none" id="showRemind">
      <label class="col-md-2 control-label">
        <a href="javascript:void(0);" onclick="fn_members_dialog_select('remindUser', 'remindUserId')" style="color: #4FA5D0">抄送人员</a>
        <i class="fa fa-asterisk txt-color-red"></i>
      </label>
      <div class="col-md-6">
        <input type="text" id="remindUser" name="remindUser" class="form-control" placeholder="请选择抄送人员" readonly value="" onclick="fn_members_dialog_select('remindUser', 'remindUserId')">
        <input type="hidden" id="remindUserId" name="remindUserId" value="">
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-2 control-label">
        下一步流程
      </label>
      <div class="col-md-6">
        <div class="radio">
          <input type="radio" id="nextNo" name="next" value="0" class="magic-radio">
          <label for="nextNo">归档</label>
          <input type="radio" id="nextYes" name="next" value="1" class="magic-radio">
          <label for="nextYes">选择下一步操作人</label>
        </div>
      </div>
    </div>

    <div class="form-group" style="display:none" id="showNextStep">
      <label class="col-md-2 control-label">
        <a href="javascript:void(0);" onclick="fn_member_dialog_select('editors', 'editorsId')" style="color: #4FA5D0">选择下一步操作人</a>
        <i class="fa fa-asterisk txt-color-red"></i>
      </label>
      <div class="col-md-6">
        <input type="text" id="editors" name="editors" class="form-control" placeholder="请选择下一步操作人" readonly onclick="fn_member_dialog_select('editors', 'editorsId')" >
        <input type="hidden" id="editorsId" name="editorsId">
      </div>
    </div>

  </form>
  <!--===================================================-->
  <!-- END BASIC FORM ELEMENTS -->

  <div class="panel-footer">
    <a class="btn btn-default" id="apply-re-common" href="javascript:void(0)">
      <i class="fa fa-lg fa-mail-reply"></i> 返回
    </a>
    <a class="btn btn-success" id="btn-save-common" href="javascript:void(0);"><i class="fa fa-lg fa-download"></i> 保存 </a>
    <c:if test="${goodsApply==null || goodsApply.processState=='Draft'}">
      <a class="btn btn-warning" id="btn-commit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 提交 </a>
    </c:if>
    <c:if test="${goodsApply!=null && goodsApply.processState=='Backed'}">
      <a class="btn btn-warning" id="btn-recommit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 再提交 </a>
    </c:if>
  </div>
</div>

<script>
  var index = "${index}";
  var todo = "${todo}";
  var remind = "${remind}";
  var record = "${record}";
  var draft =  "${draft}";

  $(function(){
    fn_goodsApply_input_validate()
  });

  //下一步点击
  $(':radio[name="next"]').click(function(){
    if($(this).val() == "1"){
      $('#showNextStep').show();
      $('.useDiv').hide();
    }else{
      $('#showNextStep').hide();
      $('.useDiv').show();
    }
  });

  //抄送点击
  $(':radio[name="remind"]').click(function(){
    if($(this).val() == "1"){
      $('#showRemind').show();
    }else{
      $('#showRemind').hide();
    }
  });

  //增加行
  $("#add_line").click(function(){
    var newTr='<tr>'
            +'<td><input type="text" class="form-control" name="name" /></td>'
            +'<td><input onkeyup="calculate(this)" type="number" class="form-control" name="price" /></td>'
            +'<td><input onkeyup="calculate(this)" type="number" class="form-control" name="count" /></td>'
            +'<td><input disabled type="number" class="form-control" name="total" /></td>'
            +'<td><btn onClick="delLine(this)" class="btn btn-danger btn-sm pull-right">删除</btn></td>'
            +'</tr>';
    $("#goodsApplyTbody").append(newTr);
  });

  //删除行
  function delLine(delBtn){
    if($("#goodsApplyTbody tr").size()==1){
      __toastr_warning("至少要有一条");
    }else{
      $(delBtn).parent().parent().remove();
      getNumMoney();
    }
  }

  //自动计算
  function calculate(input){
    var price=$(input).parent().parent().find("input[name='price']").val();
    var count=$(input).parent().parent().find("input[name='count']").val();
    var totalInput=$(input).parent().parent().find("input[name='total']");
    if(price!="" && count!=""){
      totalInput.val((price*100)*(count*100) / 10000);
    }else{
      totalInput.val(0);
    }

    getNumMoney();
  }

  //计算合计金额
  function getNumMoney(){
    var num=0;
    $("#goodsApplyTbody input[name='total']").each(function(){
      num=accAdd(num, parseFloat($(this).val()));
    });
    $("#numMoney").val(num);
  }

  function accAdd(arg1,arg2){
    var r1,r2,m;
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2));
    return (arg1*m+arg2*m)/m;
  }

  $('#useDate').datepicker({
    format: 'yyyy-mm-dd',
    weekStart: 1,
    autoclose: true,
    todayBtn: 'linked',
    todayHighlight: true,
    language: 'zh-CN'
  });

  // 表单数据校验
  function fn_goodsApply_input_validate() {
    __init_validate('form_goodsApply_input', {
      rules : {
        purpose : {
          required : true
        },
        next:{
          required:true
        },
        editorsId:{
          required:function(){
            if($(':checked[name="next"]').val() == "1"){
              return true;
            }else{
              return false;
            }
          }
        },
        useDate : {
          required:function(){
            if($(':checked[name="next"]').val() == "0"){
              return true;
            }else{
              return false;
            }
          }
        },
        useCondition : {
          required:function(){
            if($(':checked[name="next"]').val() == "0"){
              return true;
            }else{
              return false;
            }
          }
        }
      },
      messages : {
        purpose : {
          required : "请输入物品用途"
        },
        next:{
          required : "请选择下一步流程"
        },
        editorsId : {
          required : "请选择下一步操作人"
        },
        useDate:{
          required : "请选择领用时间"
        },
        useCondition : {
          required : "请输入领用情况"
        }
      }
    });
  }

  function checkTable(){
    var isOk=true;
    $("#goodsApplyTbody input").each(function(){
      if($(this).val()==""){
        isOk=false;
      }
    });
    return isOk;
  }

  //保存
  $("#btn-save-common").click(function(){

    if(!checkTable()){
      __toastr_error("表格内容请填写完整");
      return false;
    }

    __confirm_dialog("提示","确定保存吗？",function(){
      __form_save("form_goodsApply_input", "<%=path%>/com/goodsApply/save",function(){
        $("#apply-re-common").trigger("click");
      })
    })
  });

  //提交
  $("#btn-commit-common,#btn-recommit-common").click(function(){

    if(!checkTable()){
      __toastr_error("表格内容请填写完整");
      return false;
    }

    if (!$('#form_goodsApply_input').valid()) {
      return false;
    }

    __confirm_dialog("提示","确定提交吗？",function(){
      __form_save("form_goodsApply_input", "<%=path%>/com/goodsApply/commit",function(){
        $("#apply-re-common").trigger("click");
      })
    })
  });


  //返回视图
  $("#apply-re-common").click(function(){
    if(index==1){
      loadURL("menu/dashboard",$('#page-content'));
    }else if(todo==1){
      loadURL("menu/toDo",$('#page-content'));
    }else if(remind==1){
      loadURL("menu/remind",$('#page-content'));
    }else if(record==1){
      loadURL("menu/taskRecord?type=1",$('#page-content'));
    }else if(record==2){
      loadURL("menu/taskRecord?type=2",$('#page-content'));
    }else if(draft==1){
      loadURL("menu/draft",$('#page-content'));
    }else{
      returnToHashUrl()
    }
  });

</script>